<template>
    <Modal :mask-closable="false"
            v-model="resultVisble"
            class="modal-content-layout"
            :title="`${$t('preliminary.FinalAcceptanceTest')}`"
    >
        <Form ref="resultAcceptCheckForm" :model="formTop" label-position="top">
          <FormItem :label="`${$t('preliminary.FinalAcceptanceTest')}:`" required>
                <RadioGroup v-model="formTop.finalAcceptResult">
                    <Radio v-for="item in resultList" :label="item.id" :key="item.id">{{ item.name }}</Radio>
                </RadioGroup>
                <Input v-model="formTop.finalAcceptRemark" type="textarea" :rows="4" placeholder="请填写说明"/>
            </FormItem>
<!--            <FormItem label="设备管理员">-->
<!--                <Input v-model="formTop.belongCompanyAdminName" disabled/>-->
<!--            </FormItem>-->
          <FormItem :label="`${$t('preliminary.UserDepartmentOwner')}:`" required>
                <Select
                        v-model="formTop.belongShowName"
                        filterable
                        clearable
                        :placeholder="`${$t('common.selectInfo')}`"
                        :remote-method="getOAList"
                        :loading="loading"
                        class="Input-wrapper"
                >
                    <Option v-for="(item,index) of OAList" :value="item.showName" :key="index">{{ item.showName }}</Option>
                </Select>
            </FormItem>
          <FormItem :label="`${$t('preliminary.EHSDepartmentOwner')}:`" required>
                <Select
                        v-model="formTop.safeUserName"
                        filterable
                        clearable
                        :placeholder="`${$t('common.selectInfo')}`"
                        :remote-method="getOAList"
                        :loading="loading"
                        class="Input-wrapper"
                >
                    <Option v-for="(item,index) of OAList" :value="item.showName" :key="index">{{ item.showName }}</Option>
                </Select>
            </FormItem>
          <FormItem :label="`${$t('preliminary.QualityDepartmentOwner')}:`" required>
                <Select
                        v-model="formTop.qualityUserName"
                        filterable
                        clearable
                        :placeholder="`${$t('common.selectInfo')}`"
                        :remote-method="getOAList"
                        :loading="loading"
                        class="Input-wrapper"
                >
                    <Option v-for="(item,index) of OAList" :value="item.showName" :key="index">{{ item.showName }}</Option>
                </Select>
            </FormItem>
          <FormItem :label="`${$t('preliminary.ProjectOwner')}:`" required>
            <Select
                v-model="formTop.projectUserName"
                filterable
                clearable
                :placeholder="`${$t('common.selectInfo')}`"
                :remote-method="getOAList"
                :loading="loading"
                class="Input-wrapper"
            >
              <Option v-for="(item,index) of OAList" :value="item.showName" :key="index">{{ item.showName }}</Option>
            </Select>
          </FormItem>
          <FormItem :label="`${$t('ledger.Others')}:`" required>
           <Input v-model="formTop.otherUserName"/>
<!--            <Select-->
<!--                v-model="formTop.otherUserName"-->
<!--                filterable-->
<!--                clearable-->
<!--                placeholder="请选择项目负责人"-->
<!--                :remote-method="getOAList"-->
<!--                :loading="loading"-->
<!--                class="Input-wrapper"-->
<!--            >-->
<!--              <Option v-for="(item,index) of OAList" :value="item.showName" :key="index">{{ item.showName }}</Option>-->
<!--            </Select>-->
          </FormItem>
          <FormItem :label="`${$t('preliminary.Vendor')}:`" required>
            <Input v-model="formTop.supplier" disabled/>
          </FormItem>
          <FormItem :label="`${$t('preliminary.FinalAcceptanceTest')}:`" required>
              <div style="display: flex; justify-content: flex-start;">
                <Button type="primary" @click="getDowntemplate">{{$t('preliminary.DownloadTemplate')}}</Button>
                <Upload
                    ref="fileUpload"
                    :show-upload-list="false"
                    :before-upload="handleXnBeforeUpload"
                    type="drag"
                    action=""
                    style="width: 70px"
                >
                  <Button type="primary">{{$t('preliminary.Upload')}}</Button>
                </Upload>
                <div class="file-list" v-for="(item, index) in xnFiles" :key="index" >
                  <div class="item-src">{{ item.fileName }}</div>
                  <Icon class="item-icon" type="ios-close" size="20" style="float:right;" @click="delXNFileList(index)" v-if="item"></Icon>
                </div>
              </div>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="default" @click="handleCancel">{{$t('common.Cancle')}}</Button>
            <Button type="primary" :loading="loadingVisible" @click="handleOk">{{$t('maintenance.Confirmed')}}</Button>
        </div>
    </Modal>
</template>

<script>
    import fileDownload from "js-file-download";
    export default {
        name: "resultFinalCheckModal",
        data() {
            return {
              reAcceptanceTimes:'',
                loading: false,
                resultVisble: false,
                loadingVisible: false,
                formTop: {},
                OAList: [],
                resultList: [{id: 1, name: this.$t('acceptanceCheck.Qualified')}, {id: 2, name:  this.$t('acceptanceCheck.Unqualified')}],
                xnFiles: [],
                otherFiles: [],
                aFiles: [],
                gFiles: [],
                gjFiles: [],//随机工具/备件文件
                jsFiles: [],//技术文件
                cjFiles: [],//厂家文件
            }
        },
        methods: {
          getDowntemplate(){
            this.axios({
              method: 'get',
              url: '/dms/dmsCheck/exportFatWord',
              responseType: 'blob',
            }).then(res => {
              fileDownload(res, this.$t('common.template') + '.docx')
            })
          },
            //获取人员数据
            getOAList(query) {
                if (query !== '') {
                    this.loading = true
                    this.axios({
                        method: 'get',
                        url: '/basic/user/searchOAUser',
                        params: {
                            key: query
                        }
                    }).then(res => {
                        if (res.code === 200) {
                            this.OAList = res.data.map(item => {
                                return {
                                    ...item,
                                    showName: item.empno + '----' + item.username
                                }
                            })
                        } else {
                            this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                        }
                    }).finally(() => {
                        this.loading = false
                    })
                }else {
                    this.OAList = []
                }
            },
            show(record) {
                this.formTop = Object.assign({},record)
                this.xnFiles = []
                // this.otherFiles = []
                // this.aFiles = []
                // this.gFiles = []
                // this.gjFiles = []
                // this.cjFiles = []
                // this.jsFiles = []
                this.resultVisble = true
                this.getDetail(record)

            },
            getDetail(record) {
                this.axios({
                    method: 'post',
                    url: '/dms/dmsCheck/get',
                    params: {
                        deviceMId: record.deviceMId
                    }
                }).then(res => {
                    
                    if (res.code == 200) {
                      if (res.data.reAcceptanceTimes){
                        this.reAcceptanceTimes = res.data.reAcceptanceTimes
                      }
                        // this.$nextTick(() => {
                        //     this.xnFiles = res.data.xnysFile
                        //     this.otherFiles = res.data.otherFile
                        //     this.aFiles = res.data.ajdFile
                        //     this.gFiles = res.data.gjdFile
                        //     this.gjFiles = res.data.sjfjFile
                        //     this.cjFiles = res.data.cjpxFile
                        //     this.jsFiles = res.data.jszlFile
                        // })
                    }
                })
            },
            //随机工具/备件文件上传
            handleGJBeforeUpload(file) {
                let formData = new FormData();
                formData.append("dir", 'device')
                formData.append("file",file)
                let config = {
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                };
                this.axios({
                    method: 'post',
                    url: '/fileServer/upload',
                    data: formData,
                    config,
                }).then(res => {
                    if (res.code == 200) {
                        this.gjFiles.push({
                            // attachmentType: 10,
                            fileName: res.data.fileName,
                            filePath: res.data.filepath,
                            fileUrl: res.data.fileUrl
                        })
                    }
                })
                return false;
            },
            //技术附件上传
            handleJSBeforeUpload(file) {
                let formData = new FormData();
                formData.append("dir", 'device')
                formData.append("file",file)
                let config = {
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                };
                this.axios({
                    method: 'post',
                    url: '/fileServer/upload',
                    data: formData,
                    config,
                }).then(res => {
                    if (res.code == 200) {
                        this.jsFiles.push({
                            // attachmentType: 2,
                            fileName: res.data.fileName,
                            filePath: res.data.filepath,
                            fileUrl: res.data.fileUrl
                        })
                    }
                })
                return false;
            },
            //厂家文件上传
            handleCJBeforeUpload(file) {
                let formData = new FormData();
                formData.append("dir", 'device')
                formData.append("file",file)
                let config = {
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                };
                this.axios({
                    method: 'post',
                    url: '/fileServer/upload',
                    data: formData,
                    config,
                }).then(res => {
                    if (res.code == 200) {
                        this.cjFiles.push({
                            // attachmentType: 12,
                            fileName: res.data.fileName,
                            filePath: res.data.filepath,
                            fileUrl: res.data.fileUrl
                        })
                    }
                })
                return false;
            },
            //文件上传
            handleXnBeforeUpload(file) {
                let formData = new FormData();
                formData.append("dir", 'device')
                formData.append("file",file)
                let config = {
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                };
                this.axios({
                    method: 'post',
                    url: '/fileServer/upload',
                    data: formData,
                    config,
                }).then(res => {
                    if (res.code == 200) {
                        this.xnFiles.push({
                            // attachmentType: 8,
                            fileName: res.data.fileName,
                            filePath: res.data.filepath,
                            fileUrl: res.data.fileUrl
                        })
                    }
                })
                return false;
            },
            //文件上传
            handleOtherBeforeUpload(file) {
                let formData = new FormData();
                formData.append("dir", 'device')
                formData.append("file",file)
                let config = {
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                };
                this.axios({
                    method: 'post',
                    url: '/fileServer/upload',
                    data: formData,
                    config,
                }).then(res => {
                    if (res.code == 200) {
                        this.otherFiles.push({
                            // attachmentType: 9,
                            fileName: res.data.fileName,
                            filePath: res.data.filepath,
                            fileUrl: res.data.fileUrl
                        })
                    }
                })
                return false;
            },
            //文件上传
            handleABeforeUpload(file) {
                let formData = new FormData();
                formData.append("dir", 'device')
                formData.append("file",file)
                let config = {
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                };
                this.axios({
                    method: 'post',
                    url: '/fileServer/upload',
                    data: formData,
                    config,
                }).then(res => {
                    if (res.code == 200) {
                        this.aFiles.push({
                            // attachmentType: 13,
                            fileName: res.data.fileName,
                            filePath: res.data.filepath,
                            fileUrl: res.data.fileUrl
                        })
                    }
                })
                return false;
            },
            //文件上传
            handleGBeforeUpload(file) {
                let formData = new FormData();
                formData.append("dir", 'device')
                formData.append("file",file)
                let config = {
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                };
                this.axios({
                    method: 'post',
                    url: '/fileServer/upload',
                    data: formData,
                    config,
                }).then(res => {
                    if (res.code == 200) {
                        this.gFiles.push({
                            // attachmentType: 14,
                            fileName: res.data.fileName,
                            filePath: res.data.filepath,
                            fileUrl: res.data.fileUrl
                        })
                    }
                })
                return false;
            },
            //文件预览
            handlePreview(item) {
                if (item) {
                    window.open(this.util.filePathToUrl(item.filePath))
                }
            },
            //工具备件文件删除
            delGJFileList(id) {
                this.gjFiles = this.gjFiles.filter((item, index) => index !=id)
            },
            //技术资料删除
            delJSFileList(id) {
                this.jsFiles = this.jsFiles.filter((item, index) => index !=id)
            },
            //厂家文件删除
            delCJFileList(id) {
                this.cjFiles = this.cjFiles.filter((item, index) => index !=id)
            },
            //性能文件删除
            delXNFileList(id) {
                this.xnFiles = this.xnFiles.filter((item, index) => index !=id)
            },
            //其他性能文件删除
            delOtherFileList(id) {
                this.otherFiles = this.otherFiles.filter((item, index) => index !=id)
            },
            //A文件删除
            delAFileList(id) {
                this.aFiles = this.aFiles.filter((item, index) => index !=id)
            },
            //G文件删除
            delGFileList(id) {
                this.gFiles = this.gFiles.filter((item, index) => index !=id)
            },
            handleOk() {
                if (!this.formTop.safeUserName || !this.formTop.finalAcceptResult ||
                    !this.formTop.belongShowName || !this.formTop.qualityUserName
                    ) {
                    this.$Message.warning(({content: this.$t('acceptanceCheck.inputAllInfo'),duration:6,closable:true}));
                    return false
                }
                if (!(this.xnFiles && this.xnFiles.length)){
                  this.$Message.warning(({content:this.$t('acceptanceCheck.placeUploadOrder'),duration:6,closable:true}));
                  return
                }
                if(this.formTop.finalAcceptResult == 2 && !this.formTop.finalAcceptRemark) {
                    this.$Message.warning(({content: this.$t('acceptanceCheck.noPassReason'),duration:6,closable:true}));
                    return false
                }
                //性能文件
                let xnFiles = this.xnFiles.map(item => {
                    return {
                        ...item,
                        attachmentType: 8,
                    }
                })
                //其他文件
                // let otherFiles = this.otherFiles.map(item => {
                //     return {
                //         ...item,
                //         attachmentType: 9,
                //     }
                // })
                // //A文件
                // let aFiles = this.aFiles.map(item => {
                //     return {
                //         ...item,
                //         attachmentType: 13,
                //     }
                // })
                // //G文件
                // let gFiles = this.gFiles.map(item => {
                //     return {
                //         ...item,
                //         attachmentType: 14,
                //     }
                // })
                // //随机备件
                // let gjFiles = this.gjFiles.map(item => {
                //     return {
                //         ...item,
                //         attachmentType: 10,
                //     }
                // })
                // //厂家文件
                // let cjFiles = this.cjFiles.map(item => {
                //     return {
                //         ...item,
                //         attachmentType: 12,
                //     }
                // })
                // //技术附件
                // let jsFiles = this.jsFiles.map(item => {
                //     return {
                //         ...item,
                //         attachmentType: 2,
                //     }
                // })
                //使用部门负责人
                let belongIndex = this.formTop.belongShowName.indexOf('----')
                let belongDeptAdminNo = this.formTop.belongShowName.substr(0,belongIndex)
                let belongDeptAdminName = this.formTop.belongShowName.substr(belongIndex+4,this.formTop.belongShowName.length-1)

                //安全部门负责人
                let factoryIndex = this.formTop.safeUserName.indexOf('----')
                let safeUserNo = this.formTop.safeUserName.substr(0,factoryIndex)
                let safeUserName = this.formTop.safeUserName.substr(factoryIndex+4,this.formTop.safeUserName.length-1)
                //质量部门负责人
                let accuracyIndex = this.formTop.qualityUserName.indexOf('----')
                let qualityUserNo = this.formTop.qualityUserName.substr(0,accuracyIndex)
                let qualityUserName = this.formTop.qualityUserName.substr(accuracyIndex+4,this.formTop.qualityUserName.length-1)
                 //项目负责人
                 let accuracyIndexx = this.formTop.projectUserName.indexOf('----')
                 let projectUserNo = this.formTop.projectUserName.substr(0,accuracyIndexx)
                 let projectUserName = this.formTop.projectUserName.substr(accuracyIndex+4,this.formTop.projectUserName.length-1)

                this.loadingVisible = true
                let dmsAttachmentList = [ ...xnFiles]
                let params = {

                    id:this.formTop.id,
                    belongDeptAdminNo,
                    belongDeptAdminName,

                    safeUserNo,
                    safeUserName,

                    qualityUserNo,
                    qualityUserName,

                    projectUserNo,
                    projectUserName,
                    otherUserName:this.formTop.otherUserName,
                   // supplier:this.formTop.supplier,
                    reAcceptanceTimes:this.reAcceptanceTimes,
                    dmsAttachmentList,
                    deviceMId: this.formTop.deviceMId,
                    finalAcceptResult: this.formTop.finalAcceptResult,
                    finalAcceptRemark: this.formTop.finalAcceptRemark,
                }
                this.axios({
                    method: 'post',
                    url: '/dms/dmsCheck/update',
                    data: params
                }).then(res => {
                    if(res.code === 200){
                        this.$emit('handleFresh')
                        this.resultVisble = false
                        this.$refs.resultAcceptCheckForm.resetFields();
                        this.$Message.success(({content:this.$t('acceptanceCheck.checkSuccess'),duration:3,closable:true}));
                    }else{
                        this.$Message.warning(({content:res.msg,duration:6,closable:true}));
                    }
                }).finally(() => {
                    this.loadingVisible = false
                })
            },
            handleCancel() {
                this.resultVisble = false
            }
        }
    }
</script>

<style scoped lang="scss">
/deep/.ivu-upload-drag{
  border: none !important;
}
    .file-list {
        margin: 0 20px;
        .item-src {
            width: 80%;
            display: inline-block;
            line-height: 26px;
            color: #187bdb;
            cursor: pointer;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .item-icon {
            width: 20%;
            display: inline-block;
            margin-top: 4px;
        }
    }
</style>
